package com.smart.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smart.model.customdo.ProjectInfoDO;
import com.smart.model.domain.ProjectDO;
import com.smart.model.request.OperateProjectRequest;
import com.smart.model.request.ProjectFileTaskRequest;
import com.smart.model.request.ProjectRequest;
import com.smart.model.request.ProjectWeeklyTaskRequest;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * @author zyh
 */
@Repository
public interface ProjectMapper extends BaseMapper<ProjectDO> {
    ProjectDO selectDOById(Long id);
    List<ProjectDO> selectDOList(ProjectDO project);
    Long insertDO(ProjectDO project);
    int batchInsertDO(List<ProjectDO> projectDOList);
    int updateDO(ProjectDO project);
    int deleteDOById(Long id);
    int deleteDOByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from project",
            "   <where>",
            "       deleted=0",
            "       <if test=\"1==0\">and id=#{param.id}</if>",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") ProjectRequest param);
    @Select({"<script>",
            "   select * from project",
            "   <where>",
            "       deleted=0",
            "       <if test=\"1==0\">and id=#{param.id}</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<ProjectDO> pageQuery(@Param("param") ProjectRequest param, @Param("limit") int limit, @Param("offset") long offset);

    @Select({"<script>",
            "   select count(1) from project",
            "   <where>",
            "       deleted=0",
            "       and id in",
            "       <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"param.keyWord!=null and param.keyWord!=''\">",
            "           and (project_name like concat('%',#{param.keyWord},'%') or project_number like concat('%',#{param.keyWord},'%') or quality_registration_number like concat('%',#{param.keyWord},'%'))",
            "       </if>",
            "       <if test=\"param.streetCode!=null and param.streetCode!=''\">and street_code=#{param.streetCode}</if>",
            "       <if test=\"param.projectStatus!=null and param.projectStatus!=''\">and construction_status=#{param.projectStatus}</if>",
            "   </where>",
            "</script>"})
    long weeklyPageCount(@Param("param") ProjectWeeklyTaskRequest param);

    /**
     * 按照传入的in的顺序排序
     * @param param
     * @param limit
     * @param offset
     * @return
     */
    @Select({"<script>",
            "   select * from project",
            "   <where>",
            "       deleted=0",
            "       and id in",
            "       <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"param.keyWord!=null and param.keyWord!=''\">",
            "           and (project_name like concat('%',#{param.keyWord},'%') or project_number like concat('%',#{param.keyWord},'%') or quality_registration_number like concat('%',#{param.keyWord},'%'))",
            "       </if>",
            "       <if test=\"param.streetCode!=null and param.streetCode!=''\">and street_code=#{param.streetCode}</if>",
            "       <if test=\"param.projectStatus!=null and param.projectStatus!=''\">and construction_status=#{param.projectStatus}</if>",
            "   </where>",
            "   order by field(id, ",
            "       <foreach collection=\"param.projectIds\" item=\"item\" open=\"\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "   limit #{limit} offset #{offset}",
            "</script>"})
    List<ProjectDO> weeklyPageQuery(@Param("param") ProjectWeeklyTaskRequest param, @Param("limit") int limit, @Param("offset") long offset);

    /**
     * 按照传入的in的顺序排序
     * @param param
     * @return
     */
    @Select({"<script>",
            "   select * from project",
            "   <where>",
            "       deleted=0",
            "       and id in",
            "       <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"param.keyWord!=null and param.keyWord!=''\">",
            "           and (project_name like concat('%',#{param.keyWord},'%') or project_number like concat('%',#{param.keyWord},'%') or quality_registration_number like concat('%',#{param.keyWord},'%'))",
            "       </if>",
            "       <if test=\"param.streetCode!=null and param.streetCode!=''\">and street_code=#{param.streetCode}</if>",
            "       <if test=\"param.projectStatus!=null and param.projectStatus!=''\">and construction_status=#{param.projectStatus}</if>",
            "   </where>",
            "   order by field(id, ",
            "       <foreach collection=\"param.projectIds\" item=\"item\" open=\"\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "</script>"})
    List<ProjectDO> weeklyListQuery(@Param("param") ProjectWeeklyTaskRequest param);


    int selectProNum(@Param("projectNumber") String projectNumber,@Param("id") Long id);

    int cloudUpdateByKey(ProjectDO projectDO);

    @Select({"<script>",
            "   select * from project p",
            "   inner join jianshe_project jp on p.id=jp.project_id",
            "   <where>",
            "       p.deleted=0 and jp.deleted=0 and jp.jianshe_unit_id=#{jiansheUnitId}",
            "   </where>",
            "</script>"})
    List<ProjectDO> listByJianSheId(Long jiansheUnitId);

    List<ProjectDO> selectAllNewProject();

    ProjectDO selectNewProject(Long id);

    List<ProjectDO> selectByProjectName(ProjectRequest request);

    List<HashMap<String,Object>> selectByProjectNumber(ProjectRequest request);

    List<ProjectDO> supervisePageQueryRecords(ProjectRequest request);

    List<ProjectDO> supervisePageQueryRecords2(ProjectRequest request);

    ProjectDO selectByUserInfo(@Param("account") String account, @Param("password")String password);

    List<ProjectDO> selectAllPassProject(ProjectRequest request);

    List<ProjectDO> selectAllExamineProject(ProjectRequest request);

    List<ProjectDO> selectProjectListByHeadInfo(ProjectRequest request);

    @Select({"<script>",
            "   select * from project",
            "   <where>",
            "       deleted=0",
            "       and id in",
            "       <foreach collection=\"projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"constructionStatus!=null and constructionStatus!=''\">and construction_status=#{constructionStatus}</if>",
            "       <if test=\"streetCode!=null and streetCode!=''\">and street_code=#{streetCode}</if>",
            "   </where>",
            "</script>"})
    List<ProjectDO> listByProjectIds(@Param("projectIds") Set<Long> projectIds, @Param("constructionStatus") String constructionStatus, @Param("streetCode") String streetCode);
    @MapKey("id")
    @Select({"<script>",
            "   select * from project",
            "   <where>",
            "       deleted=0",
            "       and id in",
            "       <foreach collection=\"projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "   </where>",
            "</script>"})
    Map<Long, ProjectDO> listDOByProjectIds(@Param("projectIds") Set<Long> projectIds);
    @Select({"<script>",
            "   select * from project",
            "   <where>",
            "       deleted=0",
            "       and id in",
            "       <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"param.streetCode!=null and param.streetCode!=''\">and street_code=#{param.streetCode}</if>",
            "       <if test=\"param.projectStatus!=null and param.projectStatus!=''\">and construction_status=#{param.projectStatus}</if>",
            "   </where>",
            "</script>"})
    List<ProjectDO> listByRequest(@Param("param") ProjectWeeklyTaskRequest request);

    @Select("select * from project where project_number=#{code} or quality_registration_number=#{code} limit 1")
    ProjectDO queryProject(String code);

    List<String> selectAllProjectNumber(@Param("projectId")Long projectId);

    @Select({"<script>",
            "   select count(1) from project",
            "   <where>",
            "       deleted=0",
            "       and id in",
            "       <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"param.streetCode!=null and param.streetCode!=''\">and street_code=#{param.streetCode}</if>",
            "       <if test=\"param.projectStatus!=null and param.projectStatus!=''\">and construction_status=#{param.projectStatus}</if>",
            "   </where>",
            "</script>"})
    long filePageCount(@Param("param") ProjectFileTaskRequest param);
    @Select({"<script>",
            "   select * from project",
            "   <where>",
            "       deleted=0",
            "       and id in",
            "       <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"param.streetCode!=null and param.streetCode!=''\">and street_code=#{param.streetCode}</if>",
            "       <if test=\"param.projectStatus!=null and param.projectStatus!=''\">and construction_status=#{param.projectStatus}</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<ProjectDO> filePageQuery(@Param("param") ProjectFileTaskRequest param, @Param("limit") int limit, @Param("offset") long offset);

    @Select({"<script>",
            "   select * from project",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.keyWord!=null and param.keyWord!=''\">",
            "           and (project_name like concat('%',#{param.keyWord},'%') or project_number like concat('%',#{param.keyWord},'%') or quality_registration_number like concat('%',#{param.keyWord},'%'))",
            "       </if>",
            "       <if test=\"param.engineeringType!=null and param.engineeringType!=''\">and engineering_type=#{param.engineeringType}</if>",
            "       <if test=\"param.constructionStatus!=null and param.constructionStatus!=''\">and construction_status=#{param.constructionStatus}</if>",
            "       <if test=\"param.streetCode!=null and param.streetCode!=''\">and street_code=#{param.streetCode}</if>",
            "   </where>",
            "   order by create_time desc",
            "</script>"})
    List<ProjectDO> listCondition(@Param("param") OperateProjectRequest request);


    @Select({"<script>",
            "   select * from project",
            "   <where>",
            "       deleted=0",
            "       and id in",
            "       <foreach collection=\"param.projectIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "           #{item} ",
            "       </foreach> ",
            "       <if test=\"param.streetCode!=null and param.streetCode!=''\">and street_code=#{param.streetCode}</if>",
            "       <if test=\"param.projectStatus!=null and param.projectStatus!=''\">and construction_status=#{param.projectStatus}</if>",
            "   </where>",
            "   order by create_time desc ",
            "</script>"})
    List<ProjectDO> fileExportListQuery(@Param("param") ProjectFileTaskRequest param);

    List<String> selectAllBuildStatus();

    List<ProjectDO> selectBuildStatus();

    List<String> selectAllBuildType();

    List<String> selectAllProjectAccount(@Param("projectId")Long projectId);

    List<String> selectDefultProjectNumber();

    List<ProjectDO> selectByStatus();

    List<ProjectDO> selectStartProject();

    List<ProjectDO> selectMultiplexProjectList();

    List<ProjectDO> selectNotMultiplexProjectList();

    List<Map<String,Long>> selectUpProjectList();

    List<Map<String,Long>> selectDownProjectList();

    List<ProjectInfoDO> list();

    List<ProjectDO> listAll();

    List<ProjectDO> selectProjectByStatus();


    List<ProjectDO> selectProjectByTime(@Param("startTime") Date startTime);

    List<ProjectDO> selectProjectByParames();

    Integer selectNewProjectByTime(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<Long> selectRunningProjectCount();

    List<ProjectDO> listUnReportByReportIdAndItemType(@Param("taskId") Long taskId, @Param("itemType") String itemType);

    /**
     * 统计项目状态为非完工状态的数量
     * @return
     */
    @Select("select count(1) from project where DELETED=0 and valid=1 and construction_status != 'FINISH';")
    Integer countOnlineProject();

    @Select("select construction_status, engineering_type from project where DELETED=0 and valid=1 and construction_status = #{constructionStatus}")
    List<ProjectDO> listByStatus(String constructionStatus);

    List<ProjectDO> listStopAndRunProject();
}


